package com.salon.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.salon.modules.sys.entity.SysUserRoleEntity;
import com.salon.modules.sys.mapper.SysUserRoleMapper;
import com.salon.modules.sys.service.ISysUserRoleService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

/**
 * 用户与角色对应关系
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends
        ServiceImpl<SysUserRoleMapper, SysUserRoleEntity> implements ISysUserRoleService {

    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        // 先删除用户与角色关系
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("user_id", userId);
        this.removeByMap(paramMap);
        if (roleIdList == null || CollectionUtils.isEmpty(roleIdList)) {
            return;
        }
        // 保存用户与角色关系
        for (Long roleId : roleIdList) {
            SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
            sysUserRoleEntity.setUserId(userId);
            sysUserRoleEntity.setRoleId(roleId);
            this.save(sysUserRoleEntity);
        }
    }

    @Override
    public List<Long> listRoleIdByUserId(Long userId) {
        return baseMapper.listRoleIdByUserId(userId);
    }

    @Override
    public int deleteBatch(Long[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }
}
